package cn.com.shopec.erp.product.commo;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Component;

import cn.com.shopec.erp.common.page.PageFinder;
import cn.com.shopec.erp.common.utils.LogicUtil;
import cn.com.shopec.erp.common.utils.StringUtil;
import cn.com.shopec.erp.product.dao.ICommodityCatDao;
import cn.com.shopec.erp.product.entity.Commodity;
import cn.com.shopec.erp.product.entity.CommodityCat;
import cn.com.shopec.erp.product.entity.ProdSpecValue;
import cn.com.shopec.erp.product.entity.ProdSupplier;
import cn.com.shopec.erp.product.entity.Product;
import cn.com.shopec.erp.product.entity.SpecValOption;
import cn.com.shopec.erp.product.entity.Specification;
import cn.com.shopec.erp.product.service.IBrandService;
import cn.com.shopec.erp.product.service.impl.CommodityServiceImpl;
import cn.com.shopec.erp.product.vo.CommodityVo;
import cn.com.shopec.erp.product.vo.ProdSpecValueVo;
import cn.com.shopec.erp.product.vo.ProdSupplierVo;
import cn.com.shopec.erp.product.vo.ProductVo1;
import cn.com.shopec.erp.product.vo.SpecValOptionVo;
import cn.com.shopec.erp.product.vo.SpecificationVo;

@Component
public class CommodityVoConvertor {
	private static final Log log = LogFactory
			.getLog(CommodityVoConvertor.class);

	@Resource
	private IBrandService brandService;

	@Resource
	private ICommodityCatDao commodityCatDao;

	/**
	 * 
	 * @Title: convert2BaseCommodity
	 * @Description: 基本商品信息的commodityVo转commodity
	 * @param commodityVo
	 * @return
	 * @throws
	 * @author: duhui
	 * @date: 2013-5-29下午07:15:47
	 */
	public Commodity convert2BaseCommodity(CommodityVo commodityVo) {
		Commodity commodity = null;

		if (LogicUtil.isNotNull(commodityVo)) {
			commodity = new Commodity();

			commodity.setCatNo(commodityVo.getCatNo());
			commodity.setBrandNo(commodityVo.getBrandNo());
			commodity.setCommoName(commodityVo.getCommoName());
			commodity.setCommoType(commodityVo.getCommoType());
			commodity.setDescription(commodityVo.getDescription());

		}

		return commodity;
	}

	public Commodity convert2BaseCommodity(CommodityVo commodityVo,
			Commodity commodity) {

		if (LogicUtil.isNotNull(commodityVo)) {
			commodity = new Commodity();

			commodity.setCatNo(commodityVo.getCatNo());
			commodity.setBrandNo(commodityVo.getBrandNo());
			commodity.setCommoName(commodityVo.getCommoName());
			commodity.setCommoType(commodityVo.getCommoType());
			commodity.setDescription(commodityVo.getDescription());

		} else {
			return null;
		}

		return commodity;
	}

	public PageFinder<CommodityVo> convert2PagedCommodityVos(
			PageFinder<Commodity> pagedCommoditys) {
		PageFinder<CommodityVo> pagedCommodityVos = null;

			List<CommodityVo> commodityList = null;
			if (LogicUtil.isNotNullAndEmpty(pagedCommoditys.getData())) {
				commodityList = this.convert2BaseCommodityVos(pagedCommoditys
						.getData());
			}

			pagedCommodityVos = new PageFinder<CommodityVo>(
					pagedCommoditys.getPageNo(), pagedCommoditys.getPageSize(),
					pagedCommoditys.getRowCount(), commodityList);

		return pagedCommodityVos;
	}

	public List<CommodityVo> convert2BaseCommodityVos(
			List<Commodity> commoditylist) {
		List<CommodityVo> commodityVolist = new ArrayList<CommodityVo>();

			if (LogicUtil.isNotNull(commoditylist)) {
				for (Commodity commodity : commoditylist) {
					commodityVolist
							.add(this.convert2BaseCommodityVo(commodity));
				}
			}

		return commodityVolist;
	}

	public CommodityVo convert2BaseCommodityVo(Commodity commodity) {
		CommodityVo commodityVo1 = null;

		if (LogicUtil.isNotNull(commodity)) {
			commodityVo1 = new CommodityVo();
			commodityVo1.setCommoNo(commodity.getCommoNo());
			commodityVo1.setCommoName(commodity.getCommoName().trim());
			commodityVo1.setCommoType(commodity.getCommoType() == null ? 0
					: commodity.getCommoType());
			commodityVo1.setDescription(commodity.getDescription());
			commodityVo1.setBrandNo(commodity.getBrandNo());
			commodityVo1.setCheckState(commodity.getCheckState() == null ? 0
					: commodity.getCheckState());
			commodityVo1.setCatNo(commodity.getCatNo());
			commodityVo1.setCheckReason(commodity.getCheckReason());
			commodityVo1.setCreateTime(commodity.getCreateTime());
			commodityVo1.setUpdateTime(commodity.getUpdateTime());
			if (StringUtil.isExist(commodity.getBrandNo())) {
				String brandName = this.brandService.getBrand(
						commodityVo1.getBrandNo()).getBrandName();
				commodityVo1.setBrandName(brandName);
				CommodityCat commodityCat = this.commodityCatDao.findUniqueBy(
						"catNo", commodity.getCatNo());
				if (LogicUtil.isNotNull(commodityCat)) {
					commodityVo1.setCatName(commodityCat.getCatName());
				}
			}

		}

		return commodityVo1;
	}

	/**
	 * 
	 * @Title: convert2BaseProduct
	 * @Description: 货品基本信息的productVo装product
	 * @param productVo
	 * @return
	 * @throws
	 * @author: duhui
	 * @date: 2013-5-29下午07:20:57
	 */
	public Product convert2BaseProduct(ProductVo1 productVo) {
		Product product = null;

		if (LogicUtil.isNotNull(productVo)) {
			product = new Product();

			product.setProdNo(productVo.getProdNo());
			product.setProdBarCode(productVo.getProdBarCode());
			product.setInventoryType(productVo.getInventoryType());
			product.setSalePrice(productVo.getSalePrice());
			product.setCasePakgSize(product.getCasePakgSize());

		}

		return product;
	}

	public ProductVo1 convert2BaseProductVo(Product product) {
		ProductVo1 productVo = null;

		if (LogicUtil.isNotNull(product)) {
			productVo = new ProductVo1();

			productVo.setProdNo(product.getProdNo());
			productVo.setCommoNo(product.getCommoNo());
			productVo.setProdBarCode(product.getProdBarCode());
			productVo.setInventoryType(product.getInventoryType());
			productVo.setSalePrice(product.getSalePrice());
			productVo.setCasePakgSize(product.getCasePakgSize());
			productVo.setSaleType(product.getSaleType());
			productVo.setSpecification(product.getSpecification());
			productVo.setUnSalePrice(product.getUnSalePrice());
			productVo.setProdRate(product.getProdRate());
			productVo.setBuyNum(product.getBuyNum());
			productVo.setProdUnit(product.getProdUnit());
		}

		return productVo;
	}

	public List<ProductVo1> convert2BaseProductVos(List<Product> products) {
		List<ProductVo1> productVos = null;

		if (LogicUtil.isNotNullAndEmpty(products)) {
			productVos = new ArrayList<ProductVo1>();

			for (Product product : products) {
				ProductVo1 productVo = this.convert2BaseProductVo(product);
				productVos.add(productVo);
			}

		}

		return productVos;
	}

	/**
	 * 
	 * @Title: convert2BaseProdSupplier
	 * @Description: 货品供应商基本信息的prodSupplierVo转prodSupplier
	 * @param productVo
	 * @return
	 * @throws
	 * @author: duhui
	 * @date: 2013-5-29下午07:25:39
	 */
	public ProdSupplier convert2BaseProdSupplier(ProdSupplierVo prodSupplierVo) {
		ProdSupplier prodSupplier = null;

		if (LogicUtil.isNotNull(prodSupplierVo)) {
			prodSupplier = new ProdSupplier();

			prodSupplier.setSupplierNo(prodSupplierVo.getSupplierNo());
			prodSupplier.setUntaxCostPrice(prodSupplierVo.getUntaxCostPrice());
			prodSupplier.setTaxCostPrice(prodSupplierVo.getTaxCostPrice());
			prodSupplier.setGrossMargin(prodSupplierVo.getGcrossMargin());
			prodSupplier.setIsReturnedGoos(prodSupplierVo.getIsReturnedGoos());

		}

		return prodSupplier;
	}

	public List<CommodityVo> convert2CommodityVos(List<Commodity> commodities) {
		List<CommodityVo> commodityVos = null;

		if (LogicUtil.isNotNullAndEmpty(commodities)) {
			commodityVos = new ArrayList<CommodityVo>();

			for (Commodity commodity : commodities) {
				commodityVos.add(this.convert2BaseCommodityVo(commodity));
			}
		}

		return commodityVos;
	}

	public SpecificationVo convert2SpecificationVo(Specification specification) {
		SpecificationVo specificationVo = null;

		if (LogicUtil.isNotNull(specification)) {
			specificationVo = new SpecificationVo();

			specificationVo.setSpecNo(specification.getSpecNo());
			specificationVo.setSpecName(specification.getSpecName());
			specificationVo.setCatNo(specification.getCatNo());
			specificationVo.setDeleteFlag(specification.getDeleteFlag());
		}

		return specificationVo;
	}

	public List<SpecificationVo> convert2SpecificationVos(
			List<Specification> specifications) {
		List<SpecificationVo> specificationVos = null;

		if (LogicUtil.isNotNullAndEmpty(specifications)) {
			specificationVos = new ArrayList<SpecificationVo>();

			for (Specification specification : specifications) {
				SpecificationVo specificationVo = this
						.convert2SpecificationVo(specification);
				List<SpecValOption> specValOption = specification.getOptions();
				if (LogicUtil.isNotNullAndEmpty(specValOption)) {
					List<SpecValOptionVo> specValOptionVo = this
							.convert2SpecValOptionVos(specValOption);
					specificationVo.setOptions(specValOptionVo);
				}

				specificationVos.add(specificationVo);

			}
		}

		return specificationVos;
	}

	public List<SpecValOptionVo> convert2SpecValOptionVos(
			List<SpecValOption> specValOptions) {
		List<SpecValOptionVo> specValOptionVos = null;

		if (LogicUtil.isNotNullAndEmpty(specValOptions)) {
			specValOptionVos = new ArrayList<SpecValOptionVo>();

			for (SpecValOption specValOption : specValOptions) {
				SpecValOptionVo specValOptionVo = this
						.convert2SpecValOptionVo(specValOption);

				specValOptionVos.add(specValOptionVo);

			}
		}

		return specValOptionVos;
	}

	public SpecValOptionVo convert2SpecValOptionVo(SpecValOption specValOption) {
		SpecValOptionVo specValOptionVo = null;

		if (LogicUtil.isNotNull(specValOption)) {
			specValOptionVo = new SpecValOptionVo();

			specValOptionVo.setOptionNo(specValOption.getOptionNo());
			specValOptionVo.setOptionName(specValOption.getOptionName());
			specValOptionVo.setDeleteFlag(specValOption.getDeleteFlag());
		}

		return specValOptionVo;
	}

	public List<ProdSpecValueVo> convert2ProdSpecValues(
			List<ProdSpecValue> prodSpecValues) {
		List<ProdSpecValueVo> prodSpecValueVos = null;

		if (LogicUtil.isNotNullAndEmpty(prodSpecValues)) {
			prodSpecValueVos = new ArrayList<ProdSpecValueVo>();

			for (ProdSpecValue prodSpecValue : prodSpecValues) {
				ProdSpecValueVo prodSpecValueVo = this
						.convert2ProdSpecValue(prodSpecValue);
				prodSpecValueVos.add(prodSpecValueVo);
			}
		}

		return prodSpecValueVos;
	}

	public ProdSpecValueVo convert2ProdSpecValue(ProdSpecValue prodSpecValue) {
		ProdSpecValueVo prodSpecValueVo = null;

		if (LogicUtil.isNotNull(prodSpecValue)) {
			prodSpecValueVo = new ProdSpecValueVo();

			prodSpecValueVo.setProdNo(prodSpecValue.getProdNo());
			prodSpecValueVo.setSpecNo(prodSpecValue.getSpecNo());
			prodSpecValueVo.setValOptionNo(prodSpecValue.getValOptionNo());
		}

		return prodSpecValueVo;

	}

}
